package yxy.game.pm2.command.jadeseal;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.medalgrade;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.JadeSealMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.jadeseal.JadeSeal;
import yxy.game.pm2.module.jadeseal.JadeSealModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.Collection;
import java.util.Date;

/** 玉玺 阵营玉玺升级 */
@MessageType(proto = Command.JadeSealLevelUp, classtype = JadeSealMessage.JadeSealLevelUpRequest.class)
public class CMD_JadeSealLevelUp extends Command<JadeSealMessage.JadeSealLevelUpRequest> {

	@Override
	public void execute(MessageSource messageSource, JadeSealMessage.JadeSealLevelUpRequest request) {
		Date date = new Date();
		String playerCode = messageSource.getTag();
		int camp = request.getCamp();
		JadeSealMessage.JadeSealLevelUpResponse.Builder response = JadeSealMessage.JadeSealLevelUpResponse.newBuilder();
		response.setCamp(camp);
		
		JadeSealModule jsmodule = JadeSealModule.get(playerCode);
		if(jsmodule == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		JadeSeal js = jsmodule.getJss().get(camp);
		if(js == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(!js.isOpen()) {
			send(messageSource, response, Result.JadeSealNeedUnlock);
			return;
		}
		
		medalgrade mg = DataCenter.getData(js.countmgid(), medalgrade.class);
		if(mg == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		Collection<Currency> costcurs = Currency.create(mg.getCosttype(), mg.getCostid(), mg.getCostnum());
		try {
			new CurrencyHandle().cost(playerCode, CCReason.玉玺, costcurs);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
			return;
		}
		
		int newexp = js.getExp() + mg.getExp();
		if(newexp >= mg.getExpup()) {
			int nextmgid = js.countnextmgid();
			medalgrade nextmg = DataCenter.getData(nextmgid, medalgrade.class);
			if(nextmg == null) {
				newexp = mg.getExpup();
			}else {
				newexp = 0;
				js.setLevel(nextmg.getLevel());
			}
		}
		js.setExp(newexp);
		jsmodule.set();


		PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
		PlayTypeHandle.saveJadeSeal(playerCode,playTypeModule);//提前把战力算到中间缓存

		new BattleHandle().powerUpdate(playerCode);// 战力更新
		
		response.setLevel(js.getLevel());
		response.setExp(js.getExp());
		send(messageSource, response, Result.Success);

		Date date1 = new Date();
		System.out.println("CMD_JadeSealLevelUp:"+(date1.getTime()-date.getTime()));
	}

	private void send(MessageSource messageSource, JadeSealMessage.JadeSealLevelUpResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}